﻿using System.Text;
using System.Collections.Generic;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace SilverPart
{
    public partial class SilverPartWebPart : WebPart
    {
        #region Scripts Factory Methods

        private string CreateSilverlightDivHostFactory(SilverlightVersion version)
        {
            string currentUser = string.Empty;

            string currentSite = string.Empty;

            StringBuilder builder = new StringBuilder();

            builder.Append("<div id=\"" + this.silverlightControlHost + "\" style=\"border:0px;width:100%;height:100%\">");
            if (version == SilverlightVersion.Version2)
            {
                builder.Append("<object id=\"" + this.silverlightControlID + "\" data=\"data:application/x-silverlight-2,\" type=\"application/x-silverlight-2\" width=\"" + this.silverlightControlWidth + "\" height=\"" + this.silverlightControlHeight + "\">");
            }
            else
            {
                builder.Append("<object id=\"" + this.silverlightControlID + "\" data=\"data:application/x-silverlight\" type=\"application/x-silverlight\" width=\"" + this.silverlightControlWidth + "\" height=\"" + this.silverlightControlHeight + "\">");
            }
            builder.Append("<param value=\"" + this.silverlightControlBackground + "\" name=\"background\"/>");
            builder.Append("<param value=\"" + this.silverlightControlWindowless + "\" name=\"Windowless\"/>");
            builder.Append("<param value=\"" + this.silverlightControlHtmlAccess.ToString() + "\" name=\"enableHtmlAccess\"/>");
            builder.Append("<param value=\"" + this.silverlightControlSource + "\" name=\"source\"/>");

            Dictionary<string, string> kv = new Dictionary<string, string>();

            //InitParams section
            if (!string.IsNullOrEmpty(this.silverlightControlInitParams))
            {
                string[] initParams = this.silverlightControlInitParams.Split(',');
                foreach (string p in initParams) 
                {
                    if (!string.IsNullOrEmpty(p))
                    {
                        string key = p.Split('=')[0];
                        string value = p.Split('=')[1];
                        if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                        {
                            kv.Add(key, value);
                        }                        
                    }
                }
            }   
            
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (this.SilverlightControlCurrentUser)
                {
                    if (SPContext.Current.Web.CurrentUser != null)
                    {
                        currentUser = SPContext.Current.Web.CurrentUser.LoginName;
                        if (!string.IsNullOrEmpty(currentUser))
                        {
                            kv.Add("currentUser", currentUser);
                        }
                    }
                    else
                    {
                        kv.Add("currentUser", "anonymous");
                    }
                }

                if (this.SilverlightControlCurrentSite)
                {
                    currentSite = SPContext.Current.Web.Url;
                    if (!string.IsNullOrEmpty(currentSite))
                    {
                          kv.Add("currentSite", currentSite);
                    }
                 }
             });

            if (kv.Count > 0)
            {
                builder.Append("<param value=\"");
                foreach (KeyValuePair<string, string> kvp in kv)
                {
                    builder.Append(kvp.Key.ToString() + "=" + kvp.Value.ToString() + ",");
                }
                builder.Append("\" name=\"initParams\"/>");
           }
     
            builder.Append("<param value=\"" + this.silverlightControlHtmlAccess.ToString() + "\" name=\"enableHtmlAccess\"/>");

            if (this.silverlightInstallBanner)
            {
                builder.Append("<a href=\"" + this.silverlightInstallURL + "\" style=\"text-decoration: none;\">");
                builder.Append("<img src=\"" + this.silverlightInstallImage + "\" alt=\"Get Microsoft Silverlight\"");
                builder.Append("style=\"border-style: none\" /></a>");
            }
            else
            {
                builder.Append("<p>Cannot instantiate Silverlight content. Make sure you have the right Silverlight plugin installed. Please contact your administrator.</p>");
            }

            builder.Append("</object>");
            builder.Append("</div>");

            return builder.ToString();
        }

        #endregion
    }
}